package cn.iocoder.yudao.module.member.controller.app.auth;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.member.controller.admin.auth.vo.*;
import cn.iocoder.yudao.module.member.controller.app.auth.vo.AppAuthLoginReqVO;
import cn.iocoder.yudao.module.member.controller.app.auth.vo.AppAuthLoginRespVO;
import cn.iocoder.yudao.module.member.convert.auth.MemberAuthConvert;
import cn.iocoder.yudao.module.member.enums.MemberAuthBizTypeEnum;
import cn.iocoder.yudao.module.member.enums.MemberAuthShopBizTypeEnum;
import cn.iocoder.yudao.module.member.service.auth.DayuAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 会员认证大愚学员")
@RestController
@RequestMapping("/member/auth/dayu")
@Validated
@Slf4j
public class AppMemberAuthController {

    @Resource
    DayuAuthService dayuAuthService;

    @PostMapping("/member/create")
    @Operation(summary = "会员认证大愚学员")
    public CommonResult<Long> memberAuth(@RequestBody @Valid MemberAuthCreateReqVO reqVO) {
        return success(dayuAuthService.createAuth(reqVO));
    }

    @PostMapping("/member/update")
    @Operation(summary = "会员认证大愚学员")
    public CommonResult<Long> memberAuth(@RequestBody @Valid MemberAuthUpdateReqVO reqVO) {
        reqVO.setStatus(MemberAuthBizTypeEnum.AUTH.getType());
        reqVO.setReason("");
        dayuAuthService.updateAuth(reqVO);
        return success(reqVO.getId());
    }

    @GetMapping("/member/detail")
    @Operation(summary = "获取认证信息")
    public CommonResult<MemberAuthRespVO> getMemberAuth(@RequestParam("memberId") Long memberId){

        return success(MemberAuthConvert.INSTANCE.convert(dayuAuthService.getAuthByMemberId(memberId)));
    }

    @PostMapping("/shop/create")
    @Operation(summary = "创建会员认证店铺")
    public CommonResult<Long> createShopAuth(@Valid @RequestBody MemberShopAuthCreateReqVO createReqVO) {
        return success(dayuAuthService.createShopAuth(createReqVO));
    }

    @PostMapping("/shop/update")
    @Operation(summary = "修改会员认证店铺")
    public CommonResult<Long> updateShopAuth(@Valid @RequestBody MemberShopAuthUpdateReqVO updateReqVO) {
        updateReqVO.setStatus(MemberAuthShopBizTypeEnum.AUTH.getType());
        dayuAuthService.updateShopAuth(updateReqVO);
        return success(updateReqVO.getId());
    }

    @GetMapping("/shop/detail")
    @Operation(summary = "获取认证信息")
    public CommonResult<MemberShopAuthRespVO> getShopAuth(@RequestParam("memberId") Long memberId){
        return success(MemberAuthConvert.INSTANCE.convertShop(dayuAuthService.getMemberShopAuthByMemberId(memberId)));
    }

}
